package com.kit.personalkit.controller;

import com.kit.personalkit.common.JsonResponseBody;
import com.kit.personalkit.common.ResponseStatus;
import com.kit.personalkit.mapper.ScheduleMapper;
import com.kit.personalkit.pojo.dto.ScheduleDTO;
import com.kit.personalkit.pojo.entity.Schedule;
import com.kit.personalkit.pojo.vo.ScheduleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>项目名称: com.kit.personalkit </p>
 * <p>文件名称: ScheduleController </p>
 * <p>描述: 日程管理模块控制器类 </p>
 * <p>创建时间: 2023/5/30 </p>
 * <p>公司信息: java1班</p>
 * @author YinXi
 * @version 1.0
 * @since 1.8
 */
@RestController
@RequestMapping("/schedule/")
public class ScheduleController {
    /**自动装配ScheduleMapper根据Mybatis获取对应的JDBC代码*/
    @Autowired
    private ScheduleMapper scheduleMapper;

    /**
     * <h3>添加日程功能</h3>
     * @Title: addSchedule
     * @Description:
     * 接收浏览器端通过POST：/schedule/add传送过来的添加一条日志的数据写入数据库中并且向浏览器端返回状态码和schedueleVO对象信息
     * @Param: ScheduleDTO前端传递过来被封装的数据
     * @Return: JsonResponseBody
     * @author: YinXi
     * @see ScheduleMapper
     * @see ScheduleDTO
     * @see ScheduleVO
     * @see com.kit.personalkit.config.MybatisConfig
     * @see Schedule
     * @see JsonResponseBody
     * @see ResponseStatus
     */
    @PostMapping (value = "add")
    public JsonResponseBody addSchedule(@RequestBody ScheduleDTO scheduleDTO){
        Schedule schedule = new Schedule();
        /*
        1,检测用户是否登录
         */
        if(scheduleDTO.getId() == null){
            return new JsonResponseBody(ResponseStatus.NOT_LOGIN);
        }
        //设置用户的id
        schedule.setCreatorId(scheduleDTO.getId());
        /*
        2,检测上传的时间是否为空或null并且对时间进行格式的转换来避免出现一些无法预知的问题
         */
        //设置创建时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = sdf.format(date);
        schedule.setDateTime(dateTime);
        //获取前端传递的时间数组，包含开始时间和结束时间并且取出对应的值
        String[] time = scheduleDTO.getTime();
        if(time != null && time.length>0){
            String startTime = time[0];
            String endTime = time[1];
            schedule.setStartTime(startTime);
            schedule.setEndTime(endTime);
        }else {
            return new JsonResponseBody(ResponseStatus.NO_TIME);
        }
        schedule.setReminderTime(scheduleDTO.getReminderTime());
        //设置内容
        schedule.setScheduleContent(scheduleDTO.getScheduleContent());
        /*
        3，插入数据并且返回成功时的响应
         */
        scheduleMapper.insertSchedule(schedule);
        ScheduleVO scheduleVO = scheduleMapper.selectSchedule(schedule.getCreatorId(),schedule.getDateTime());
        return new JsonResponseBody(ResponseStatus.ADD_SCHEDULE_SUCCESS,scheduleVO);
    }

    /**
     * <h3>根据id查询对应的数据库信息</h3>
     * @Title: getScheduleById
     * @Description:
     * 接收浏览器端通过Get:/schedule/getById?id=xx获取前端传递的scheduleId,根据所获取的id从数据库中查询对应的信息后进行返回
     * @Param: id 获取前端传送的id
     * @Return: JsonResponseBody
     * @author: YinXi
     * @see ScheduleMapper
     * @see ScheduleDTO
     * @see ScheduleVO
     * @see com.kit.personalkit.config.MybatisConfig
     * @see JsonResponseBody
     * @see ResponseStatus
     */
    @GetMapping(value = "getById")
    public JsonResponseBody getScheduleById(int id){
        ScheduleDTO scheduleDTO = new ScheduleDTO();
        scheduleDTO.setId(id);
        ScheduleVO scheduleVO = scheduleMapper.selectById(scheduleDTO.getId());
        if(scheduleVO == null){
            return new JsonResponseBody(ResponseStatus.NO_SCHEDULE);
        }
        return new JsonResponseBody(ResponseStatus.SUCCESS);
    }

    /**
     * <h3>修改日程功能-根据新的日程信息修改日程</h3>
     * @Title: alertSchedule
     * @Description:
     * 接收浏览器端通过Put:/schedule/alterSchedule/id获取前端传递的scheduleId和封装的修改信息ScheduleDTO,
     * 根据id和DTO中的信息对数据库中对应id的信息进行修改并将修改后的内容进行返回
     * @Param: id 获取前端传送的id,scheduleDTO 前端传递过来被封装的DTO内容
     * @Return: JsonResponseBody
     * @author: YinXi
     * @see ScheduleMapper
     * @see ScheduleDTO
     * @see ScheduleVO
     * @see com.kit.personalkit.config.MybatisConfig
     * @see Schedule
     * @see JsonResponseBody
     * @see ResponseStatus
     */
    @PutMapping(value = "alertSchedule/{id}/alter")
    public JsonResponseBody alertSchedule(@PathVariable("id") int id,@RequestBody ScheduleDTO scheduleDTO){
        Schedule schedule = new Schedule();
        /*
        1,根据传递的id值查找是否存在数据
         */
        if(scheduleMapper.selectById(id) == null){
            return getScheduleById(id);
        }
        //设置shceduleid
        schedule.setScheduleId(id);
        /*
        2,根据ScheduleDTO传递过来的时间数组判断时间数组中是否存在数据并且设置数据
         */
        schedule.setScheduleContent(scheduleDTO.getScheduleContent());
        String[] time = scheduleDTO.getTime();
        if(time != null && time.length>0){
            String startTime = time[0];
            String endTime = time[1];
            schedule.setStartTime(startTime);
            schedule.setEndTime(endTime);
        }else {
            return new JsonResponseBody(ResponseStatus.NO_TIME);
        }
        //设置提醒时间
        schedule.setReminderTime(scheduleDTO.getReminderTime());
        //设置创建时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = sdf.format(date);
        schedule.setDateTime(dateTime);
        /*
        3，修改数据库中对应的id的数据内容并返回成功
         */
        scheduleMapper.alterById(schedule);
        ScheduleVO scheduleVO = scheduleMapper.selectById(schedule.getScheduleId());
        return new JsonResponseBody(ResponseStatus.SET_SCHEDULE_SUCCESS,scheduleVO);
    }

    /**
     * <h3>删除日程功能</h3>
     * @Title: getScheduleById
     * @Description:
     * 接收浏览器端通过Delete:/schedule/deleteById/id获取前端传递的scheduleId,根据所获取的id从数据库中
     * 查询对应的信息并进行删除操作后返回对应的状态码给前端
     * @Param: id 获取前端传送的id
     * @Return: JsonResponseBody
     * @author: YinXi
     * @see ScheduleMapper
     * @see com.kit.personalkit.config.MybatisConfig
     * @see Schedule
     * @see JsonResponseBody
     * @see ResponseStatus
     */
    @DeleteMapping(value = "deleteById/{id}/delete")
    public JsonResponseBody deleteById(@PathVariable("id") String id){
        Schedule schedule = new Schedule();
        int scheduleId = Integer.parseInt(id);
        /*
        1,判断前端传递的scheduleId是否存在
         */
        if(scheduleMapper.selectById(scheduleId)== null){
            return getScheduleById(scheduleId);
        }
        //设置schedulId
        schedule.setScheduleId(scheduleId);
        /*
        2,返回是否删除成功并且返回响应
         */
        int result = scheduleMapper.deleteById(schedule.getScheduleId());
        if(result<=0){
            return new JsonResponseBody(ResponseStatus.DELETE_FAIL);
        }else {
            return new JsonResponseBody(ResponseStatus.DELETE_SUCCESS);
        }
    }

    /**
     * <h3>获取日程列表功能</h3>
     * @Title: showSchedule()
     * @Description:
     * 接收浏览器端通过Get:/schedule/showList获取前端传递的showList请求,找到此方法后查询数据库中的信息，
     * 返回状态码和对应的数据信息
     * @Return: JsonResponseBody
     * @author: YinXi
     * @see ScheduleMapper
     * @see com.kit.personalkit.config.MybatisConfig
     * @see JsonResponseBody
     * @see ResponseStatus
     */
    @GetMapping("showList/{userId}/getUserScheduleList")
    public JsonResponseBody showSchedule(@PathVariable("userId") int id){
        ScheduleDTO scheduleDTO = new ScheduleDTO();
        scheduleDTO.setId(id);
        return new JsonResponseBody(ResponseStatus.PASS_DATA,scheduleMapper.selectAll(scheduleDTO.getId()));
    }
}